package org.elanfox.cloud.mybatis.config;

import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elanfox.cloud.mybatis.base.MybatisProperties;
import org.elanfox.cloud.mybatis.utils.EntityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.PostConstruct;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/9/1
 */
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties(MybatisProperties.class)
@MapperScan("org.elanfox.cloud.mybatis.ddl.mapper")
@Slf4j
public class MybatisConfig {
    @Autowired
    private MybatisProperties mybatisProperties;

    @PostConstruct
    public void init() throws NoSuchFieldException {
        if (StringUtils.isNotEmpty(mybatisProperties.getDdlAuto()) && !"none".equals(mybatisProperties.getDdlAuto())) {
            if (ArrayUtil.isEmpty(mybatisProperties.getEntityScan())) {
                log.info("mybatis ddl can not start, please definition fox.cloud.mybatis.entity-scan...");
                return;
            }
            EntityHelper.init(mybatisProperties.getEntityScan());
        }
    }
}
